package com.clhbolg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clhbolg.domain.UniqueView;
import com.clhbolg.domain.bo.UniqueViewBo;
import com.clhbolg.domain.vo.UniqueViewVo;
import com.clhbolg.mapper.UniqueViewMapper;
import com.clhbolg.model.dto.UniqueViewDTO;
import com.clhbolg.service.IUniqueViewService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author cailinhu
 * @date 2023-02-08
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class UniqueViewServiceImpl implements IUniqueViewService {

    private final UniqueViewMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public UniqueViewVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<UniqueViewVo> queryPageList(UniqueViewBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UniqueView> lqw = buildQueryWrapper(bo);
        Page<UniqueViewVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<UniqueViewVo> queryList(UniqueViewBo bo) {
        LambdaQueryWrapper<UniqueView> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UniqueView> buildQueryWrapper(UniqueViewBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UniqueView> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getViewsCount() != null, UniqueView::getViewsCount, bo.getViewsCount());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(UniqueViewBo bo) {
        UniqueView add = BeanUtil.toBean(bo, UniqueView.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(UniqueViewBo bo) {
        UniqueView update = BeanUtil.toBean(bo, UniqueView.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(UniqueView entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<UniqueViewDTO> listUniqueViews() {
        DateTime startTime = DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), -7));
        DateTime endTime = DateUtil.endOfDay(new Date());
        return baseMapper.listUniqueViews(startTime, endTime);
    }
}
